#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
struct edge {
    int v, u, c;
};
bool cmp(const edge &a, const edge &b) {
    return a.c < b.c;
}

const int inf = 0x3f3f3f3f;

const int max_n = 1e4 + 10;
int p[max_n];



vector<edge> e;

int find(int x) {
    if (p[x] == x) return x;
    return p[x] = find(p[x]);
}

void merge(int x, int y) {
    int fx = find(x);
    int fy = find(y);
    p[fx] = fy;
    return ;
}

int main() {
    int n, x, ans = 0;
    scanf("%d", &n);

    for(int i = 0; i <= n; i++) {
        p[i] = i;
    }

    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            scanf("%d", &x); // i->j value : x
            e.push_back((edge){i, j, x});
        }
    }

    sort(e.begin(), e.end(), cmp);
    for (int i = 0; i < e.size(); i++) {
        int v = e[i].v, u = e[i].u, c = e[i].c;
        if (find(v) == find(u)) continue;
        merge(v, u);
        ans += c;
    }
    printf("%d\n", ans);

    return 0;
}